﻿using ConfExpert.Forms.Base;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraVerticalGrid.Rows;
using DevTeam;

using System;
using System.Drawing;

namespace ConfExpert.Forms.Depo
{
    public partial class AksesuarCikisIrsaliyeleri : XForm
    {
        public AksesuarCikisIrsaliyeleri()
        {
            InitializeComponent();
        }

        private void AksesuarCikisIrsaliyeleri_Load(object sender, EventArgs e)
        {
            this.SuspendLayout();
            gvIrsaliye.Init("GetAllAksesuarIrsaliyeFiltered", 0, 0, string.Empty, 0, 0, 0, 0, 0, DateTime.Now, DateTime.Now, 0);
            gvIrsaliye.AddDetailGrid(gvIrsaliyeLines, "AksesuarIrsaliyeId");

            gvIrsaliye.AddColumn("ToplamAdet", typeof(int), "SUM(Child({FK}).CikanAdet)");

            gvIrsaliye.MakeColumnInvisible("AksesuarIrsaliyeId");
            gvIrsaliyeLines.MakeColumnInvisible("AksesuarIrsaliyeId", "Hit");
            //gvIrsaliye.SetCaptions("UserId:Talep Eden");

            gvIrsaliye.FormatAsNumber(0, "ToplamAdet");
            gvIrsaliyeLines.FormatAsNumber(0, "CikanAdet");

            gvIrsaliye.AddSummary("ToplamAdet");
            gvIrsaliyeLines.AddSummary("CikanAdet");

            splitContainerControl1.AutoSize();

            btnGetir.Click += new EventHandler(btnGetir_Click);
            btnTemizle.Click += new EventHandler(btnTemizle_Click);

            CreateVerticalGrid();

            this.ResumeLayout();

        }

        void btnGetir_Click(object sender, EventArgs e)
        {
            Rfrsh();
        }

        private void Rfrsh()
        {
            gvIrsaliye.RefreshDataSource(true, "GetAllAksesuarIrsaliyeFiltered",
                intParse(GetVal("IrsaliyeNo")),
                intParse(GetVal("TalepNo")),
                strParse(GetVal("DosyaNo")),
                intParse(GetVal("Marka")),
                intParse(GetVal("Sezon")),
                intParse(GetVal("KaynakId")),
                intParse(GetVal("CariId")),
                intParse(GetVal("TalepEden")),
                datetimeParse(GetVal("BaslangicTarihi")),
                datetimeParse(GetVal("BitisTarihi")),
                intParse(GetVal("AksesuarId"))
                );

            gvIrsaliye.BestFitColumns();
            gvIrsaliyeLines.BestFitColumns();

            Notify("Done.");
        }

        private void CreateVerticalGrid()
        {
            vgc.Rows.AddRange(new BaseRow[] {
                AddItem("IrsaliyeNo", typeof(int)),
                AddItem("TalepNo", typeof(int)),
                AddItem("DosyaNo", typeof(string)),
                AddItem("Marka", typeof(int), RI(Rp.Brand)),
                AddItem("Sezon", typeof(int), RI(Rp.Sezon)),
                AddItem("KaynakId", typeof(int), RI(Rp.Kaynak)),
                AddItem("CariId", typeof(int), RI(Rp.Cari)),
                AddItem("TalepEden", typeof(int)),
                AddItem("BaslangicTarihi", typeof(DateTime), new RepositoryItemDateEdit()),
                AddItem("BitisTarihi", typeof(DateTime), new RepositoryItemDateEdit()),
                AddItem("AksesuarId", typeof(string), RI(Rp.V_Aksesuar))
            });

            //vgc.Rows.GetRowByFieldName("Aciklama", true).Height = 80;
            vgc.OptionsBehavior.UseEnterAsTab = true;
            vgc.LayoutStyle = DevExpress.XtraVerticalGrid.LayoutViewStyle.SingleRecordView;
            vgc.CustomDrawRowHeaderCell += new DevExpress.XtraVerticalGrid.Events.CustomDrawRowHeaderCellEventHandler(vgc_CustomDrawRowHeaderCell);
            vgc.Appearance.RowHeaderPanel.BackColor = Color.LightBlue;

            vgc.SetCellValue(vgc.Rows["BaslangicTarihi"], 0, DateTime.Now.AddDays(-1));
            vgc.SetCellValue(vgc.Rows["BitisTarihi"], 0, DateTime.Now);

            vgc.FocusedRow = vgc.Rows["IrsaliyeNo"];
            vgc.BestFit();
        }

        public BaseRow AddItem(string name, Type t, params RepositoryItem[] ri)
        {
            return AddItem(vgc, name, t, ri);
        }

        object GetVal(string FieldName)
        {
            return vgc.GetCellValue(vgc.Rows[FieldName], vgc.FocusedRecord);
        }

        void btnTemizle_Click(object sender, EventArgs e)
        {
                foreach (BaseRow r in vgc.Rows)
                vgc.SetCellValue(r, 0, null);
        }
    }
}